package cn.bingo.yunsj.service;

import cn.bingo.yunsj.ascept.LogAdvice;
import cn.bingo.yunsj.common.Param;
import cn.bingo.yunsj.common.exception.EmException;
import cn.bingo.yunsj.common.exception.OwnException;
import cn.bingo.yunsj.controller.model.LinkEmployeeSendModel;
import cn.bingo.yunsj.domain.RpSuccess;
import cn.bingo.yunsj.mail.MailParam;
import cn.bingo.yunsj.mail.model.FileProperties;
import cn.bingo.yunsj.mail.model.MailModel;
import cn.bingo.yunsj.mail.model.YmlProperties;
import cn.bingo.yunsj.mapper.RpSuccessMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class MailService {

    @Autowired
    private LinkSendService linkSendService;

    @Autowired
    private RpSuccessMapper rpSuccessMapper;

    private static final Logger logger = LoggerFactory.getLogger(MailService.class);

    /**
     * 邮件发送逻辑
     */
    // to do 事务
    @Transactional
    public void send(MailModel mailModel){
        if(StringUtils.equals("聆客",mailModel.getWay())){
            sendLink(mailModel);
        }
        if(StringUtils.equals("邮件",mailModel.getWay())){
            sendMail(mailModel);
        }
        if(StringUtils.equals("ALL",mailModel.getWay())){
            sendMail(mailModel);
            logger.info("==============================");
            sendLink(mailModel);
        }
        insertInfo(mailModel);
    }

    @Transactional
    public void sendMail(MailModel mailModel){
        MailParam mailParam = new MailParam();
        logger.info("开始发送邮件");
        mailParam.sendSimpleMailMessage(mailModel);
        logger.info("邮件发送结束");
    }

    @Transactional
    public void sendLink(MailModel mailModel){
        // 防止html字符串为空
        if(StringUtils.isEmpty(FileProperties.getHtml())){
            logger.info("html字符串为空");
            FileProperties.setHtml("");
        }
        LinkEmployeeSendModel  linkEmployeeSendModel = new LinkEmployeeSendModel(){{
            // link账户
            setAccount(Param.USER_ACCOUNT);
            setUserName(Param.USER_NAME);
            setCardHtml(FileProperties.getHtml());
        }};
        logger.info("开始发送服务号");
        linkSendService.LinkMessageSend(linkEmployeeSendModel);
        logger.info("服务号发送结束");
    }

    // 存储发送成功信息
    private void insertInfo(MailModel mailModel) {
        YmlProperties ymlProperties = new YmlProperties();
        RpSuccess rpSuccess = new RpSuccess(){{
            setUpdatedBy(ymlProperties.getUsername());
            setCreatedBy(ymlProperties.getUsername());
            setUpdatedTime(new Date());
            setCreatedTime(new Date());
            setSendTime(new Date());
            setSuccessId(UUID.randomUUID().toString());
            setRecipient(mailModel.getCcEmail());
            setUserModel(mailModel.getName());
            setSender(mailModel.getEmail());
        }};
        logger.info("插入记录:" + rpSuccess);
        rpSuccessMapper.insert(rpSuccess);
    }
}
